﻿namespace SimpleWebServices.IO
{
    using System;
    using System.IO;
    using System.Text;
    using Xunit;

    public sealed class StreamExtensionMethodsFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(typeof(StreamExtensionMethods).IsStatic());
        }

        [Fact]
        public void op_ToBytes_StreamNull()
        {
            Assert.Throws<ArgumentNullException>(() => (null as Stream).ToBytes());
        }

        [Fact]
        public void op_ToBytes_StreamEmpty()
        {
            int expected = 0;
            int actual = -1;

            using (MemoryStream stream = new MemoryStream())
            {
                actual = stream.ToBytes().Length;
            }

            Assert.Equal<int>(expected, actual);
        }

        [Fact]
        public void op_ToBytes_Stream()
        {
            string expected = "abc";
            string actual = null;

            using (MemoryStream stream = new MemoryStream())
            {
                byte[] bytes = Encoding.ASCII.GetBytes(expected);
                stream.Write(bytes, 0, bytes.Length);
                stream.Flush();
                stream.Position = 0;
                actual = Encoding.ASCII.GetString(stream.ToBytes());
            }

            Assert.Equal<string>(expected, actual);
        }
    }
}